package com.imaker.webview

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.imaker.webview.callback.WebViewCallBack
import com.imaker.webview.webview.BaseWebView
import com.tencent.smtt.export.external.interfaces.WebResourceRequest
import com.tencent.smtt.sdk.WebView

/**
 * @author : dly on 2023/4/26
 *
 * desc :
 */
class WebFragment : Fragment(), WebViewCallBack {

    private lateinit var rootview: View
    private lateinit var webUrl: String
    private lateinit var baseWebView: BaseWebView

    companion object {
        fun newInstance(url: String): WebFragment {
            val bundle = Bundle()
            bundle.putString(WebConstants.URL, url)
            val fragment = WebFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            webUrl = it.getString(WebConstants.URL).toString()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        rootview = inflater.inflate(R.layout.fragment_web, container, false)
        baseWebView = rootview.findViewById(R.id.baseWebView)
        return rootview
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        baseWebView.initWebClient(this)
//        baseWebView.loadUrl(webUrl)
        baseWebView.loadUrl("${WebConstants.ANDROID_ASSET_URI}demo.html")
    }

    override fun pageStarted(url: String?) {

    }

    override fun pageFinished(url: String?) {

    }

    override fun pageError() {

    }

    override fun updateTitle(title: String?) {
    }

    override fun overrideUrlLoading(view: WebView?, url: WebResourceRequest?): Boolean {
        return false
    }
}